use sdl2::pixels::Color;
use sdl2::render::TextureQuery;
use sdl2::rect::Rect;
use sdl2::ttf::Font;
use crate::app::*;
use crate::tetris::*;
use crate::main_game::*;
use crate::constant::*;
use crate::gfx::*;
pub struct MainMenuView;

impl MainMenuView {
    pub fn new(game: &mut App) -> MainMenuView {
        MainMenuView{}
    }
}

impl View for MainMenuView {
    fn render(&mut self, game: &mut App, elapsed: f64) -> ViewAction {
        if game.events.quit || game.events.key_escape {
            return ViewAction::Quit
        }

        if game.events.now.key_n == Some(true) {
            return ViewAction::ChangeView(Box::new(GameView::new(game)))
        }

        let creator = game.canvas.texture_creator();

        {
            let op_begin_sprite = game.ttf_str_sprite(&creator, "开始(N)", 42u16, WHITE);
            let op_over_sprite = game.ttf_str_sprite(&creator, "结束(ESC)", 42u16, WHITE);

            game.canvas.set_draw_color(BLACK);
            game.canvas.clear();

            if let Some(begin_sprite) = op_begin_sprite {
                let clone = begin_sprite.tex.clone();
                //重命名，结构体字段变量
                let TextureQuery { width: width1, height, .. } = clone.borrow().query();

                let x = (WIDTH - width1) / 2;
                let y = (HEIGHT - height) / 2;

                begin_sprite.render(&mut game.canvas, Rectangle::new(x as f64, (y - 100) as f64, width1 as f64, height as f64));
            }

            if let Some(over_sprite) = op_over_sprite {
                let clone = over_sprite.tex.clone();
                //重命名，结构体字段变量
                let TextureQuery { width: width1, height, .. } = clone.borrow().query();

                let x = (WIDTH - width1) / 2;
                let y = (HEIGHT - height) / 2;

                game.canvas.copy_sprite(&over_sprite, Rectangle::new(x as f64, y as f64, width1 as f64, height as f64))
            }


        }
        ViewAction::None
    }
}
